package com.data.mining.project;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;

public class NutchIndexReader
{
	/**
	 * @param args
	 */
	@SuppressWarnings( "unchecked")
	public static void main( String[ ] args)
	{
		File dataDir = new File( args[0]);
		IndexReader reader = null;
		IndexWriter writer = null;
		Document 	stored = null;
		try
		{
			reader = IndexReader.open( dataDir);
			System.out.println( reader.numDocs( ));
			for ( int i = 0; i < reader.numDocs( ); ++i)
			{
				
				System.out.println( "##################################");
				Document document = reader.document( i);
				Iterator<Field> iterator = ( Iterator<Field>)document.getFields( ).iterator( );
				while ( iterator.hasNext( ))
				{
					Field field = iterator.next( );
					System.out.println( "Field name: " + field.name( ) + ", field value: " + field.stringValue( ));
					if ( field.name( ).indexOf( "url") >= 0)
					{
						if ( field.stringValue( ).indexOf( "thirth") >=0)
						{
							stored = document;
							System.out.println( "BINGO");
							reader.deleteDocument( i);
						}
					}
						
				}
				System.out.println( "##################################");
			}
			try
			{
				reader.close( );
			} catch ( IOException e)
			{
				e.printStackTrace();
			}
			writer = new IndexWriter( dataDir,	new StandardAnalyzer( ), false,
					               	  IndexWriter.MaxFieldLength.UNLIMITED);
			if ( stored != null)
				writer.addDocument( stored);
		} catch ( CorruptIndexException e)
		{
			e.printStackTrace();
		} catch ( IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			if ( null != writer)
			{
				try
				{
					writer.close( );
				} catch ( CorruptIndexException e)
				{
					e.printStackTrace();
				} catch ( IOException e)
				{
					e.printStackTrace();
				}
			}
		}
	}

}
